<<<<<<< HEAD
/***************************************************************************
 创建者: hualei
 开始时间: 2020-09-29
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2020.9.29 类的具体实现代码编写

 ***************************************************************************
 *  TCP以太网通信实现类                                                                       *
 ***************************************************************************/
=======
>>>>>>> origin/master
#ifndef TCPMANAGER_H
#define TCPMANAGER_H

#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
#include <QMap>
#include <QMutex>
#include <QThread>

<<<<<<< HEAD
=======
class ProgramEngine;

>>>>>>> origin/master
class TcpManager :public QTcpServer
{
    Q_OBJECT

//    QTcpSocket tmpClientSocket;
//    void test_con()
//    {
//        connect(&tmpClientSocket, static_cast<void(QAbstractSocket::*)(QAbstractSocket::SocketError)>(&QAbstractSocket::error),
//            [=](QAbstractSocket::SocketError socketError){ /* ... */ });
//    };

public:
    
    TcpManager();
    
public:
  
    int tcpStart_client(const QString& ip, int port);
    int tcpStart_server(const QString& ip, int port);
    
    int tcpWrite_client(const QString& ip, int port,const QByteArray& dataIn);
    int tcpWrite_server(const QString& ip, int port,const QByteArray& dataIn);

    int tcpRead_client(const QString& ip, int port,QByteArray& dataOut);
    int tcpRead_server(const QString& ip, int port,QByteArray& dataOut);
    
    int tcpStop_client(const QString& ip, int port);
    int tcpStop_server(const QString& ip, int port);

<<<<<<< HEAD
    int setTcpRobotId(const int &robotIdin);

protected:
=======
private:
>>>>>>> origin/master


    /**
     * @brief incomingConnection    重写此虚函数,当有连接到来的时候自动调用该函数,
     *                              但连接到来不一定连接，会根据maxPendingConnections决定是否连接
     * @param handle                句柄
     */
    virtual void incomingConnection(qintptr handle);

signals:
    void sendData_signal(const QTcpSocket &socketIn, const QByteArray &dataIn, bool &sendStatus);

    void tcpStart_Server_signal(const QString& ip, int port);

    void tcpStart_client_signal(const QString& ip, int port);

    void tcpRead_client_signal(const QString& ip,int port,QByteArray& dataOut);

    void tcpRead_server_signal(const QString &ip, int port, QByteArray &dataOut);

    void tcpWrite_clien_signal(const QString &ip, int port,const QByteArray &dataOut);

    void tcpWrite_server_signal(const QString &ip, int port,const QByteArray &dataOut);

//    /**
//     * @brief ClientConnected_Signal        客户端连入
//     * @param handle                        句柄
//     * @param clientSocket                  与连入客户端通信的Socket对象指针
//     */
//    void ClientConnected_Signal(qintptr handle, QTcpSocket* clientSocket);

    /**
     * @brief ClientDisconnected_Sianal     客户端断开链接,若需要在socket后析构后进行操作的可连接此信号
     * @param handle                        Socket对象连接句柄
     */
//    void ClientDisconnected_Sianal(qintptr handle);

private slots:

    /**
     * @brief tcpStart_client_slot
     * @param ip
     * @param port
     * @return
     */
    void tcpStart_client_slot(const QString& ip, int port);//bug,没有包含端口信息去确定server,一个ip可能多个port

    void tcpStart_server_slot(const QString& ip, int port);

    void tcpRead_Client_slot(const QString& ip,int port,QByteArray &dataOut);

    void tcpRead_Server_slot(const QString& ip,int port,QByteArray &dataOut);

    void tcpWrite_Client_slot(const QString& ip,int port,const QByteArray &dataIn);

    void tcpWrite_Server_slot(const QString& ip,int port,const QByteArray &dataIn);

//    /**
//     * @brief ClientConnected_slot        客户端连入
//     * @param handle                      句柄
//     * @param clientSocket                与连入客户端通信的Socket对象指针
//     */
//    void ClientConnected_slot(qintptr handle, QTcpSocket* clientSocket);

    /**
     * @brief ClientDisconnected_slot     客户端断开链接,若需要在socket后析构后进行操作的可连接此信号
     * @param handle                        Socket对象连接句柄
     */
//    void ClientDisconnected_slot(qintptr handle);

    /**
     * @brief sendData_slot             发送数据
     * @param socketIn                  socket通信对象
     * @param dataIn                    发送数据
     */
    void sendData_slot(QTcpSocket *socketIn, const QByteArray &dataIn, bool &sendStatus);

    void socketError_slot(QAbstractSocket::SocketError errorNum);

<<<<<<< HEAD
    void newConnectionSlot();

=======
>>>>>>> origin/master
private:

    void addMsg( int messageLevel,
                 std::string componentName,
                 std::string messageType,
                 int messageCode,
                 int robotId,
                 int parameter1 = 0,
                 int parameter2 = 0,
                 int parameter3 = 0,
                 int parameter4 = 0,
                 QString message= "");
<<<<<<< HEAD
=======

public:

    /**
     * @brief ipAndDataList     ip对应收发数据容器
     */
    QMap <QString, QByteArray> ipAndDataList;

    /**
     * @brief programEngine         程序解释引擎
     */
    ProgramEngine *programEngine;

    /**
     * @brief robotId               机器号
     */
    int robotId;
>>>>>>> origin/master
    
private:
    
    /**
     * @brief tcpServer         TCP服务器List对象(port - QTcpSever)
     */
    QMap <int, QTcpServer*> tcpServerList;
    
    /**
     * @brief tcpClientList
     */
    QMap <QString, QTcpSocket*> tcpClientList;

    /**
<<<<<<< HEAD
     * @brief serverPortMap             记录保存连接端口的用户IP
     */
    QMap <int,QString> serverPortMap;

    /**
=======
>>>>>>> origin/master
     * @brief mutex             Mutex 同步锁
     */
    QMutex mutex;

<<<<<<< HEAD
    QTcpServer *templateServer;

=======
>>>>>>> origin/master
    /**
     * @brief finishedStatus
     */
    bool readFinishedStatus;

    /**
     * @brief startFinishedStatus
     */
    bool startFinishedStatus;

    /**
     * @brief writeFinishedStatus
     */
    bool writeFinishedStatus;
<<<<<<< HEAD

    /**
     * @brief robotId               机器号
     */
    int robotId;
=======
>>>>>>> origin/master
   
};

#endif // TCPMANAGER_H
